<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>react 受控组件</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
</head>

<body>

    <div id="react"> </div>
    <!--准备好一个容器-->
    <div id="test"></div>
    <!--引入react核心库,核心库必须先引入-->
    <script type="text/javascript" src="./js/react.development.js"></script>
    <!--引入react-dom, 用于支持react操作DOM-->
    <script type="text/javascript" src="./js/react-dom.development.js"></script>
    <!--引入babel,用于将jsx转为js-->
    <script type="text/javascript" src="./js/babel.min.js"></script>

    <!-- 引入prop-types，用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="./js/prop-types.js"></script>

   <div id="react"></div>
    <script type="text/babel">

   //受控组件就是数据维护在状态state中，当需要用的时候直接从状态中取出

    class Login extends React.Component {

   //初始化状态
   state={username:'', password:''}

   //自定义赋值事件
   getUserName=(event)=>{
      const {username} =this.state 
      this.setState({username:event.target.value})
    }
    getPassWord=(event)=>{
        const {password} =this.state
        this.setState({password:event.target.value})
    }
     getSubmit=()=>{
     //原生阻止默认事件
     event.preventDefault()
     const {password,username} =this.state
     alert(`姓名${username},密码${password}`)
     }
      render() {
         return (
           <form onSubmit={this.getSubmit}>
            姓名:<input onChange={this.getUserName} type="text" name="username"/>
                <h2>{this.state.username}</h2>
            密码:<input  onChange={this.getPassWord}  type="password" name="password"/>
                <h2>{this.state.password}</h2>
                <button>登录</button>
           </form>
         )
      }

    }
   ReactDOM.render(<Login/>,document.getElementById("react"))
    </script>
    <style>
        .spacing {
            margin: 15px;
        }
    </style>

</body>

</html>